//
//  LCMyTeamViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/24.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCMyTeamViewController.h"
#import "LCFirendModel.h"
#import "LCMineService.h"
#import "LCFriendTableViewCell.h"

@interface LCMyTeamViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *todayAddNumLb;

@property (weak, nonatomic) IBOutlet UILabel *allNumLb;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCMyTeamViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getFriendNumData];
    [self setTableViewRefresh];
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getFriendData];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getFriendData];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImage:nil titleStr:@"暂无数据～" detailStr:nil];
}

- (void)getFriendNumData {
    [LCMineService friendNumberWithBlock:^(BOOL isSuccess, NSInteger todayNum, NSInteger totalNum, NSInteger todayAmount) {
        if (isSuccess) {
            self.todayAddNumLb.text = kFORMAT(@"%ld",todayNum);
            self.allNumLb.text = kFORMAT(@"%ld",totalNum);
        }
    }];
}

-(void)getFriendData {
    [LCMineService friendListWithPage:self.page Block:^(BOOL isSuccess, NSArray<LCFirendModel *> *list, NSInteger total) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (self.page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (self.dataArr.count >= total) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 76;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCFriendTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCFriendTableViewCell"];
    if (self.dataArr.count <= 1) {
        [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerAllCorners];
        cell.lineView.hidden = YES;
    } else {
        if (indexPath.row == 0) {
            [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerTopLeft|UIRectCornerTopRight];
            cell.lineView.hidden = YES;
        } else if (indexPath.row == self.dataArr.count - 1) {
            [cell.bgView quickSetViewRoundCornWithCorneradius:5 andDerection:UIRectCornerBottomLeft|UIRectCornerBottomRight];
            cell.lineView.hidden = YES;
        } else {
            [cell.bgView quickSetViewRoundCornWithCorneradius:0 andDerection:UIRectCornerAllCorners];
            cell.lineView.hidden = NO;
        }
    }
    cell.firend = self.dataArr[indexPath.row];
    return cell;
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
